package com.tsingtree.modular.controller.view;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsingtree.base.auth.context.LoginContextHolder;
import com.tsingtree.base.auth.model.LoginUser;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.NanSellAnnounce;
import com.tsingtree.modular.entity.NanSellAnnounceComment;
import com.tsingtree.modular.entity.NanSellAnnounceReply;
import com.tsingtree.modular.model.params.NanSellAnnounceCommentParam;
import com.tsingtree.modular.service.NanSellAnnounceCommentService;
import com.tsingtree.modular.service.NanSellAnnounceReplyService;
import com.tsingtree.modular.service.NanSellAnnounceService;
import com.tsingtree.roses.core.base.controller.BaseController;
import com.tsingtree.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;


/**
 * 评论控制器
 *
 * @author jagger
 */
@Controller
@RequestMapping("/nanSellAnnounceComment")
public class NanSellAnnounceCommentController extends BaseController {

    private String PREFIX = "/nanSellAnnounceComment";

    @Autowired
    private NanSellAnnounceCommentService nanSellAnnounceCommentService;
    @Autowired
    private NanSellAnnounceService nanSellAnnounceService;

    @Autowired
    private NanSellAnnounceReplyService nanSellAnnounceReplyService;

    /**
     * 跳转到主页面
     *
     * @author jagger
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/nanSellAnnounceComment.html";
    }

    /**
     * 新增页面
     *
     * @author jagger
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/nanSellAnnounceComment_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jagger
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/nanSellAnnounceComment_edit.html";
    }

    /**
     * 删除接口
     *
     * @author jagger
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(NanSellAnnounceCommentParam nanSellAnnounceCommentParam) {
        NanSellAnnounceComment nanSellAnnounceComment = nanSellAnnounceCommentService.getById(nanSellAnnounceCommentParam.getId());
        LoginUser admin = LoginContextHolder.getContext().getUser();
        if(nanSellAnnounceComment.getIsDel()){
            return ResponseData.error("该评论已被删除");
        }
        nanSellAnnounceComment.setIsDel(true);
        List<NanSellAnnounceReply> list = nanSellAnnounceReplyService.list(new QueryWrapper<NanSellAnnounceReply>()
                        .eq("is_del", false)
                .eq("comment_id", nanSellAnnounceComment.getId()));
        list.forEach(a->a.setIsDel(true));

        NanSellAnnounce nanSellAnnounce = nanSellAnnounceService.getById(nanSellAnnounceComment.getAnnounceId());
        nanSellAnnounce.setCommentTimes(nanSellAnnounce.getCommentTimes() - (1 + list.size()));
        nanSellAnnounceCommentService.del(list, nanSellAnnounceComment, nanSellAnnounce);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jagger
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(NanSellAnnounceCommentParam nanSellAnnounceCommentParam) {
        NanSellAnnounceComment detail = this.nanSellAnnounceCommentService.getById(nanSellAnnounceCommentParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jagger
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(NanSellAnnounceCommentParam nanSellAnnounceCommentParam) {
        return this.nanSellAnnounceCommentService.findPageBySpec(nanSellAnnounceCommentParam);
    }

}


